<?php

namespace common\models\base;

use common\helpers\ArrayHelper;
use common\helpers\Url;
use Yii;

/**
 * This is the model class for table "{{%base_config}}".
 *
 * @property int $id
 * @property string $scope
 * @property int $type_id 类型ID
 * @property string $name 字段名
 * @property string $format 格式
 * @property string $value 值
 * @property string $label 标签名称
 * @property string $input_type 输入框类型
 * @property string $setting 字段配置信息
 * @property string $description 描述
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class BaseConfig extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 禁用状态
     *
     * @var DISABLED_ON  0  禁用
     * @var DISABLED_OFF  10  启用
     */
    const SCOPE_SPREAD = 'spread';
    /**
     * 禁用状态
     *
     * @var DISABLED_ON  0  禁用
     * @var DISABLED_OFF  10  启用
     */
    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * 数据内容格式
     *
     * @var DISABLED_ON  0  禁用
     * @var DISABLED_OFF  10  启用
     */
    const FORMAT_STRING = 'string';
    const FORMAT_ARRAY = 'array';
    const FORMAT_IMAGE = 'image';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%base_config}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['type_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'format', 'value', 'label'], 'required'],
            [['value', 'setting'], 'string'],
            [['scope'], 'string', 'max' => 50],
            [['name', 'format', 'input_type'], 'string', 'max' => 64],
            [['label'], 'string', 'max' => 100],
            [['description'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'scope' => 'Scope',
            'type_id' => '类型ID',
            'name' => '字段名',
            'format' => '格式',
            'value' => '值',
            'label' => '标签名称',
            'input_type' => '输入框类型',
            'setting' => '字段配置信息',
            'description' => '描述',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * @param $scope
     * @return array
     * @throws \yii\base\InvalidConfigException
     */
    public static function findByScope($scope) {
        $data = static::find()->active(static::STATUS_ACTIVE)->andWhere(['scope' => $scope])->all();
        if (empty($data)) {
            return null;
        }
        $result = [];
        foreach ($data as $item) {
            if ($item->format == static::FORMAT_ARRAY) {
                $result[$item->name][] = $item->value;
            } elseif ($item->format == static::FORMAT_IMAGE) {
                $result[$item->name] = Url::attach($item->value);
            } else {
                $result[$item->name] = $item->value;
            }
        }
        return $result;
    }

}
